﻿using CheckNoteWpf.API;
using CheckNoteWpf.Data;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CheckNoteWpf
{
    public partial class InfoListItem : UserControl
    {
        public InfoListItemData Data
        {
            get => data ?? InfoListItemData.Default;
            set
            {
                if (value == null) return;
                data = value;
                CheckBoxItem.IsChecked = data.IsChecked;
                TextBoxItem.Text = data.Info;
            }
        }
        private InfoListItemData data;

        public Action<InfoListItem> OnRemoveBtnClicked;

        public Action<InfoListItem> OnTextBoxItemChanged;

        public Action<InfoListItem> OnCheckBoxItemChanged;

        private int removeBtnActiveState = 0;

        public InfoListItem(InfoListItemData data)
        {
            InitializeComponent();
            InitStyle();

            removeBtnActiveState = 0;
            RefreshRemoveBtnState();
            Data = data;
        }

        private void InitStyle()
        {
            Brush brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalDefine.GetConfigVal(ConfigKey.BgColorHex)));
            TextBoxItem.Background = brush;
            TextBoxItem.BorderBrush = brush;

            Brush btnBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalDefine.GetConfigVal(ConfigKey.CommonBtnColorHex)));
            RemoveBtn.Background = btnBrush;
            RemoveBtn.BorderBrush = null;

            Brush btnTextBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalDefine.GetConfigVal(ConfigKey.CommonBtnTextColorHex)));
            RemoveBtn.Foreground = btnTextBrush;

            Brush contentBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalDefine.GetConfigVal(ConfigKey.ContentColorHex)));
            TextBoxItem.Foreground = contentBrush;
        }

        private void RefreshRemoveBtnState()
        {
            if (removeBtnActiveState < 0) removeBtnActiveState = 0;
            RemoveBtn.Visibility = removeBtnActiveState == 0 ? Visibility.Hidden : Visibility.Visible;
        }

        private void RemoveBtn_Click(object sender, RoutedEventArgs e)
        {
            OnRemoveBtnClicked?.Invoke(this);
        }

        private void TextBoxItem_LostFocus(object sender, RoutedEventArgs e)
        {
            Data.Info = TextBoxItem.Text;
            OnTextBoxItemChanged?.Invoke(this);
            --removeBtnActiveState;
            RefreshRemoveBtnState();
        }

        private void CheckBoxItem_Checked(object sender, RoutedEventArgs e)
        {
            Data.IsChecked = CheckBoxItem.IsChecked ?? false;
            OnCheckBoxItemChanged?.Invoke(this);
        }

        private void TextBoxItem_GotFocus(object sender, RoutedEventArgs e)
        {
            RemoveBtn.Visibility = Visibility.Visible;
            ++removeBtnActiveState;
            RefreshRemoveBtnState();
        }

        private void RemoveBtn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ++removeBtnActiveState;
            RefreshRemoveBtnState();
        }

        private void RemoveBtn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            --removeBtnActiveState;
            RefreshRemoveBtnState();
        }
    }
}
